package com.ningmengs789.live.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.live.live.common.widget.gift.utils.OnItemClickListener;
import com.ningmengs789.live.net.response.PopularUser1V1Response;
import com.ningmengs789.live.server.widget.SelectableRoundedImageView;
import com.ningmengs789.live.utils.CommonUtils;
import com.bumptech.glide.Glide;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Hot1V1Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PopularUser1V1Response.ListBean> mList = new ArrayList<>();

    private int height;
    private int height2;
    private View header;

    private LayoutInflater layoutInflater;

    private Context context;

    private OnItemClickListener mOnClickListener;

    public Hot1V1Adapter(Context context, int width, View header, OnItemClickListener onItemClickListener){
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.height = ((width - CommonUtils.dip2px(context, 12)) * 142) / 710;
        this.header = header;
        mOnClickListener = onItemClickListener;
        height2 = (width - CommonUtils.dip2px(context, 18)) / 2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new HeadVh(header);
        }else if (viewType == 1){
            return new ImgVh(layoutInflater.inflate(R.layout.layout_1v1_hot_img_item, parent, false));
        }else {
            return new Vh(layoutInflater.inflate(R.layout.layout_recommended_item22, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            //banner
        } else if (getItemViewType(position) == 1) {
            if (holder instanceof ImgVh) {
                ((ImgVh) holder).setData(mList.get(position - 1));
            }
        } else {
            if (holder instanceof Vh) {
                ((Vh) holder).setData(mList.get(position - 1));
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else if (mList.get(position - 1).getData_type() == 11){
            return 1;
        }
        return 2;
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    public void setCards(List<PopularUser1V1Response.ListBean> list) {
        if (list == null) {
            return;
        }
        mList = list;
    }

    class HeadVh extends RecyclerView.ViewHolder{

        public HeadVh(View itemView) {
            super(itemView);
        }
    }

    class ImgVh extends RecyclerView.ViewHolder implements View.OnClickListener{

        SelectableRoundedImageView mIcon;

        public ImgVh(View itemView) {
            super(itemView);
            mIcon =  itemView.findViewById(R.id.icon);
            itemView.setOnClickListener(this);
        }

        void setData(PopularUser1V1Response.ListBean listBean) {
            int type = listBean.getData_type();
            if (type == 11){
                try {
                    JSONObject content = new JSONObject(listBean.getContent());

                    ViewGroup.LayoutParams params = mIcon.getLayoutParams();
                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    params.height = height;
                    mIcon.setLayoutParams(params);

                    Glide.with(context)
                            .load(CommonUtils.getUrl(content.getString("img")))
                            .into(mIcon);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onClick(View v) {
            mOnClickListener.onItemClick(null, getLayoutPosition());
        }
    }

    class Vh extends RecyclerView.ViewHolder implements View.OnClickListener{

        SelectableRoundedImageView mIcon2;
        TextView mNicknameTv2;
        Button mStateBtn2;
        TextView mStateTv2;
        TextView mVoice2;
        TextView mVideo2;

        public Vh(View itemView) {
            super(itemView);
            mIcon2 =  itemView.findViewById(R.id.recommended_item2_icon);
            mNicknameTv2 =  itemView.findViewById(R.id.recommended_item2_nickname_tv);
            mStateBtn2 = itemView.findViewById(R.id.recommended_item2_state_btn);
            mStateTv2 = itemView.findViewById(R.id.recommended_item2_state_tv);
            mVoice2 = itemView.findViewById(R.id.recommended_item2_voice_tv);
            mVideo2 = itemView.findViewById(R.id.recommended_item2_video_tv);
            itemView.setOnClickListener(this);
        }

        void setData(PopularUser1V1Response.ListBean listBean) {
            int type = listBean.getData_type();
            if (type == 2){
                try {
                    JSONObject content = new JSONObject(listBean.getContent());

                    mNicknameTv2.setText(content.getString("user_nickname"));

                    ViewGroup.LayoutParams params = mIcon2.getLayoutParams();
                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
                    params.height = height2;
                    mIcon2.setLayoutParams(params);
                    Glide.with(context)
                            .load(CommonUtils.getUrl(content.getString("show_photo")))
                            .into(mIcon2);

                    mVoice2.setText(content.getString("speech_cost") + context.getString(R.string.ql_cost));
                    mVideo2.setText(content.getString("video_cost") + context.getString(R.string.ql_cost));

                    int tint = Color.parseColor("#0aed06");
                    switch (content.getInt("online_state")) {
                        case 0:
                            tint = Color.WHITE;
                            mStateTv2.setText(context.getString(R.string.offline));
                            break;
                        case 1:
                            tint = Color.parseColor("#0aed06");
                            mStateTv2.setText(context.getString(R.string.online));
                            break;
                        case 2:
                            tint = Color.parseColor("#ff6600");
                            mStateTv2.setText("活跃");
                            break;
                        case 3:
                            tint = Color.parseColor("#0090ff");
                            mStateTv2.setText("直播中");
                            break;
                        case 4:
                            tint = Color.parseColor("#ff0000");
                            mStateTv2.setText("聊天中");
                            break;
                        case 5:
                            tint = Color.WHITE;
                            mStateTv2.setText("勿扰");
                            break;
                        default:
                            break;
                    }
                    mStateBtn2.getBackground().setColorFilter(tint, PorterDuff.Mode.SRC_ATOP);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onClick(View v) {
            mOnClickListener.onItemClick(null, getLayoutPosition());
        }
    }


}
